home *** CD-ROM | disk | FTP | other *** search
- //
- // template class RecentStates
- // Copyright (C) 1997 Kazutaka Hirata <khirata@jove.acs.unt.edu>
- //
-
- // This class is a container class useful for storing "undo"
- // and "redo" history in an application.
- //
- // Template class RecentStates requires that class T has the
- // following member functions.
- //
- // T::T()
- // T::T(const T&)
- //
- // Depending on the implementation of class list, the
- // following member functions may need to be defined but are
- // not used actually.
- //
- // bool operator==(const Document& rval) const;
- // bool operator!=(const Document& rval) const;
- // bool operator<(const Document& rval) const;
- // bool operator>(const Document& rval) const;
-
- #ifndef _RCNTSTAT_H_
- #define _RCNTSTAT_H_
-
- #include <list>
- #include <string>
-
- template<class T>
- class StateNode {
- // member variable(s)
- T m_data;
- std::string m_name;
- public:
- // constructor(s)
- StateNode() {
- }
- StateNode(const T& data, const std::string& name)
- : m_data(data),
- m_name(name) {
- }
-
- // destructor
- ~StateNode() {}
-
- // member functions
- bool operator==(const StateNode& rval) const;
- bool operator!=(const StateNode& rval) const;
- bool operator<(const StateNode& rval) const;
- bool operator>(const StateNode& rval) const;
- const T& data() const { return m_data; }
- const std::string& name() const { return m_name; }
- };
-
- template<class T>
- class RecentStates {
- // member variable(s)
- typedef StateNode<T> Node;
- typedef std::list<Node> List;
- List m_states;
- List::iterator m_current;
- std::string null_str;
- public:
- // constructor(s)
- RecentStates() {
- }
- RecentStates(const T& initial_state) {
- m_states.push_back(Node(initial_state, ""));
- m_current = m_states.begin();
- }
-
- // destructor
- virtual ~RecentStates() {}
-
- // member functions
- bool IsUndoable() const {
- return (m_current != m_states.begin()) ? true : false;
- }
- bool IsRedoable() const {
- List::iterator temp = m_current;
- temp++;
- return (temp != m_states.end()) ? true : false;
- }
- bool Undo() {
- if(!IsUndoable()) {
- return false;
- }
- m_current--;
- return true;
- }
- bool Redo() {
- if(!IsRedoable()) {
- return false;
- }
- m_current++;
- return true;
- }
- void ClearHistory() {
- if(IsUndoable()) {
- List::iterator temp = m_current;
- m_states.erase(m_states.begin(), m_current);
- }
- if(IsRedoable()) {
- List::iterator temp = m_current;
- temp++;
- m_states.erase(temp, m_states.end());
- }
- }
- bool SetNewState(const T& new_state, const std::string& name) {
- List::iterator temp = m_current;
- temp++;
- m_states.erase(temp, m_states.end());
- m_states.push_back(Node(new_state, name));
- m_current++;
- return true;
- }
- const T& GetCurrent() const { return m_current->data(); }
- const std::string& GetUndoName() const {
- if(IsUndoable()) {
- return m_current->name();
- } else {
- return null_str;
- }
- }
- const std::string& GetRedoName() const {
- if(IsRedoable()) {
- List::iterator temp = m_current;
- temp++;
- return temp->name();
- } else {
- return null_str;
- }
- }
- };
-
- #endif /* _RCNTSTAT_H_ */
-